package control
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import effect.preloader.CircleSlice.CircleSlice;
	
	import events.MyEvent;
	
	import loader.MuilteFilesLoader;
	
	import media.VideoContainer;
	
	import utils.CenterObjByNumber;
	import utils.MyTimer;

	public class Pic_flv_mp_simple_show extends Sprite
	{
		private var mutileFilesLoader:MuilteFilesLoader;
		protected var myTimer:MyTimer;//自动播放时间间隔
		protected var _wattingTime:uint=3;
		protected var touchTime:Number=0;
		protected const touchEndTime:Number=800;//触摸结束时如果小于这个时间
		protected const dragDistance:Number=50;//触摸结束时如果大于这个距离  判定为要换图
		protected var autoPlay:Boolean=false;
		protected var max_width:Number = 1366;
		protected var max_height:Number = 768;
		protected var _curNum:int = 0;
		protected var length:uint = 0;
		protected var background_color:uint;
		protected var background_alpha:Number;
		protected var loading:CircleSlice;
		protected var delayTime:uint=800;
		protected var lastMouseDownTime:uint=0;
		protected var startMouseX:Number;
		protected var startMouseY:Number;
		protected var startX:Number;
		protected var closed:Boolean=false;
		protected var canFlip:Boolean=true;
		protected var flipTimer:MyTimer;//翻页结束时间间隔
		protected var flipTime:Number=0.5;
		protected var pic_urls:Vector.<String>;
		protected var pics:Vector.<Sprite>;
		protected var first:Boolean=true;//是否是第一次
		private var wait:String = "";
		public function Pic_flv_mp_simple_show()
		{
			
		}
		
		public function autoshow():void
		{
			if(closed)return;
			autoPlay=true;
			addMyTimer();
		}
		
		public function get curNum():int
		{
			return _curNum;
		}
		
		public function set curNum(value:int):void
		{
			_curNum = value;
		}
		
		public function get wattingTime():uint
		{
			return _wattingTime;
		}
		
		public function set wattingTime(value:uint):void
		{
			_wattingTime = value;
		}
		
		public function setType(_background_color:Number=0,_background_alpha:Number=1):void
		{
			
			background_color=_background_color;
			background_alpha=_background_alpha;
		}
		
		protected function addLoading():void
		{
			if (! loading)
			{
				loading=new CircleSlice();
			}
			
			this.addChild(loading);
			loading.x = max_width / 2;
			loading.y = max_height / 2;
		}
		 
		protected function removeLoading():void
		{
			if (loading)
			{
				this.removeChild(loading);
				loading = null;
			}
		}
		
		public function init(contentArray:Vector.<String>):void
		{
			if(closed)return;
			pic_urls=contentArray;
			length = contentArray.length;
			loadFile();
		}
		
		private function loadFile():void
		{
			addLoading();
			var vec:Vector.<String>=new <String>[pic_urls[_curNum]];
			mutileFilesLoader=new MuilteFilesLoader(vec);
			mutileFilesLoader.addEventListener("complete",handleFilesLoaded);
			mutileFilesLoader.addEventListener("fileNotFound",handleFileIOEror);
			mutileFilesLoader.loadFile();
		}
		
		private function handleFilesLoaded(e:MyEvent):void
		{
			removeLoading();
			mutileFilesLoader.removeEventListener("complete",handleFilesLoaded);
			mutileFilesLoader.removeEventListener("fileNotFound",handleFileIOEror);
			mutileFilesLoader=null;
			if(!pics)
			{
				pics=new Vector.<Sprite>(length);	
			}
			if(pics[curNum])
			{
				if(this.contains(pics[curNum]))
				{
					this.removeChild(pics[curNum]);
				}
				pics[curNum]=null;
					
			}
			var obj:Object=e.obj[0];
			var sp:Sprite=new Sprite();
			sp.graphics.beginFill(0,0);
			sp.graphics.drawRect(0,0,max_width,max_height);
			sp.graphics.endFill();
			if(obj.type=="swf")
			{
				sp.addChild(obj.content);
				CenterObjByNumber.centerObj(obj.content,max_width,max_height);
			}
			else if(obj.type=="flv")
			{
				var vc:VideoContainer=new VideoContainer(max_width,max_height,false,false,0xffffff,1,false,true);
				vc.showMovie(obj.content,true,false);
				sp.addChild(vc);
			}
			else if(obj.type=="bmp")
			{
				sp.addChild(obj.content);
				CenterObjByNumber.centerObj(obj.content,max_width,max_height);
			}
			else if(obj.type=="mp")
			{
				
			}
			pics[_curNum]=sp;
			this.addChild(sp);
			
			if(first)
			{
				first=false;
				updateDisplay();
				start();
				this.dispatchEvent(new MyEvent("flipComplete",pics[curNum]));
			}
			else
			{
				showNextImg();
			}
		}
		
		public function setSize(w:Number,h:Number):void
		{
			max_width = w;
			max_height = h;
			this.graphics.clear();
			this.graphics.beginFill(background_color,background_alpha);
			this.graphics.drawRect(0,0,max_width,max_height);
			this.graphics.endFill();
			updateDisplay();
		}
		
		protected function start():void
		{
			if(closed)return;
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			
		}
		
		public function updateDisplay():void
		{
			if(closed)return;
			if(loading)
			{
				loading.x = max_width / 2;
				loading.y = max_height / 2;
			}
			if(!pics)return;
			var sp:Sprite;
			var length:uint=pics.length;
			for(var i:uint=0;i<length;i++)
			{
				sp=pics[i];
				if(sp==null)continue;
				CenterObjByNumber.centerObj(sp,max_width,max_height);
				if(i<curNum)
				{
					sp.x=-sp.width;
				}
				else if(i>curNum)
				{
					sp.x=max_width;
				}
			}
		}
		
		private function handleFileIOEror(e:MyEvent):void
		{
			trace(e.obj+" not found");
		}
		
		protected function handleMouseDown(e:MouseEvent):void
		{
			if(!canFlip)return;
			this.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			if(autoPlay)
			{
				removeMyTimer();
			}
			touchTime=getTimer();
			startMouseX=e.stageX;
			startMouseY=e.stageY;
			startX=pics[curNum].x;
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
		}
		
		
		protected function handleMouseMove(e:MouseEvent):void
		{
			if(!canFlip)return;
			
			if(closed)return;
			var distance:Number=e.stageX-startMouseX;
			pics[curNum].x=startX+distance;
			this.dispatchEvent(new MyEvent("moved",pics[curNum].x,true));//传当前对象位置出去
			if(pics[curNum].x<-max_width/2)
			{
				loadNextImg(-1);
			}
			else if(pics[curNum].x>max_width*0.5)
			{
				loadNextImg(1);
			}
		}
		
		
		protected function addFlipTimer():void
		{
			if(!flipTimer)
			{
				flipTimer=new MyTimer(setCanFlip,flipTime,1);
				flipTimer.start();
			}
		}
		
		protected function removeFlipTimer():void
		{
			if(flipTimer)
			{
				flipTimer.GC();
				flipTimer=null;
			}
		}
		
		protected function setCanFlip():void
		{
			canFlip=true;
			removeFlipTimer();
		}
		
		public function loadNextImg(distance:Number):void
		{
			if(!canFlip)return;
			canFlip=false;
			addFlipTimer();
			if(closed)return;
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			if(distance<0)
			{
				TweenLite.to(pics[curNum],0.5,{x:-pics[curNum].width,onComplete:removeLast,onCompleteParams:[curNum]});
				wait="next";
				curNum++;
				if(curNum==length)
				{
					curNum=0;
				}
			}
			else
			{
				TweenLite.to(pics[curNum],0.5,{x:max_width,onComplete:removeLast,onCompleteParams:[curNum]});
				wait="prev";
				curNum--;
				if(curNum<0)
				{
					curNum=length-1;
				}
			}
			loadFile();
		}
		
		private function removeLast(num:uint):void
		{
			var sp:Sprite=pics[num];
			var content:*=sp.getChildAt(0)
			if(content is Bitmap)
			{
				var bm:Bitmap=Bitmap(content);
				bm.bitmapData.dispose();
				sp.removeChild(bm);
				
				bm=null;
			}
			else if(content is VideoContainer)
			{
				var vc:VideoContainer=VideoContainer(content);
				vc.gc();
				sp.removeChild(vc);
				vc=null;
			}
			else if(content is Sprite)
			{
				var _sp:Sprite=Sprite(content);
				_sp.loaderInfo.loader.unloadAndStop(true);
				sp.removeChild(_sp);
			
				_sp.removeChildren();
				_sp=null;
			}
			else if(content is MovieClip)
			{
				var mc:MovieClip=MovieClip(content);
				sp.removeChild(mc);
				mc.loaderInfo.loader.unloadAndStop(true);
				mc.stopAllMovieClips();
				mc.removeChildren();
				mc=null;
			}
			
			this.removeChild(sp);
			pics[num]=null;
		}
		
		public function showNextImg():void
		{
			pics[curNum].y=max_height*0.5-pics[curNum].height*0.5;
			if(wait=="next")
			{
				pics[curNum].x=max_width;
				TweenLite.to(pics[curNum],0.5,{x:max_width/2-pics[curNum].width/2,onComplete:addMyTimer});
			}
			else if(wait=="prev")
			{
				pics[curNum].x=-pics[curNum].width;
				TweenLite.to(pics[curNum],0.5,{x:max_width/2-pics[curNum].width/2,onComplete:addMyTimer});
			}
			wait="";
			if(autoPlay)
			{
				addMyTimer();
			}
		}
		
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			var endMouseY:Number=e.stageY;
			var endMouseX:Number=e.stageX;
			var nowTime:uint=getTimer();
			//判断两次点击时间
			if(lastMouseDownTime==0)
			{
				lastMouseDownTime=nowTime;
			}
			else
			{
				if(nowTime-lastMouseDownTime <delayTime && Math.abs(endMouseY-startMouseY)<10 && Math.abs(endMouseX-startMouseX)<10)
				{
					lastMouseDownTime=0;
					//如果连续点击两次 那可能是要关闭 用于特殊用途
					this.dispatchEvent(new MyEvent("thisCanClose"));
				}
				else
				{
					lastMouseDownTime=getTimer();
				}
			}
			//===========
			if(nowTime-touchTime<touchEndTime)
			{
				var distance:Number=pics[curNum].x-startX;
				//如果是要快速切换
				if(Math.abs(distance)>dragDistance)
				{
					//确定是要切换图片了,现在判断方向
					loadNextImg(distance);
				}
				else
				{
					backpic();
				}
			}
			else 
			{
				backpic();
				
			}
			if(this.stage)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			}
			if(autoPlay)
			{
				addMyTimer();
			}
		}
		
		private function backpic():void
		{
			if(pics)
			{
				TweenLite.to(pics[curNum],0.5,{x:startX});
			}
		}
		
		protected function addMyTimer():void
		{
			
			if(closed)return;
			if(pics)
			{
				this.dispatchEvent(new MyEvent("flipComplete",pics[curNum]));
			}
			
			if(!autoPlay)return;
			removeMyTimer();
			myTimer=new MyTimer(showNext,_wattingTime);
			myTimer.start();
		}
		
		protected function showNext():void
		{
			if(closed)return;
			TweenLite.to(pics[curNum],0.5,{x:-pics[curNum].width,onComplete:removeLast,onCompleteParams:[curNum]});
			wait="next";
			curNum++;
			if(curNum==length)
			{
				curNum=0;
				
			}
			loadFile();
		}
		
		
		protected function removeMyTimer():void
		{
			if(myTimer)
			{
				myTimer.GC();
				myTimer=null;
			}
		}
		
		protected function removeSps():void
		{
			if(pics)
			{
				removeLast(curNum);
				pics=null;
			}
		}
		
		public function gc():void
		{
			closed=true;
			
			if(this.stage.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
			}
			if(this.stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			}
			removeMyTimer();
			removeFlipTimer();
			removeSps();
		}
	}
}